/**
 * Created by asus on 2018/9/22.
 */
define(['jquery', 'template', 'cookie'], function ($, template) {
  // 获取url中的参数
  var params = getRequest('id')
  var userId
  if ($.cookie('userToken') && $.cookie('userToken') !== "null") {
    console.log($.cookie('userToken'))
    userId = JSON.parse($.cookie('userToken')).id
  }
 // 获取数据，并渲染模板
  getInfo()

  // 注册点赞事件
  $("#detail-top").on('click', 'button', function () {
    var $zan = $(this).children('.iconfont')
    var $num = $(this).children('.zan-num')
    var id = params.id
    var flag = $(this).data('zan')
    $(this).toggleClass('zan')
    $(this).data('zan', !flag)
    toggleZan($zan, $num, id, !flag)
    return false
  })
  function toggleZan( $zan, $num, recordId, flag) {
    count = $num.data('count')
    if (flag) {
      $zan.removeClass('icon-zan2')
      $zan.addClass('icon-zan1')
      count += 1
    } else {
      $zan.removeClass('icon-zan1')
      $zan.addClass('icon-zan2')
      count -= 1
    }
    $num.html(count)
    $num.data('count', count)

    var data = {
      recordId: recordId,
      isZan: flag,
      userId: userId
    }
    console.log(data)
    // 发送请求
    //$.ajax({
    //  url: '/',
    //  type: 'POST',
    //  data: data,
    //  success: function (res) {
    //
    //  }
    //})
  }

  // 发布评论
  $("#comment-btn").click(function () {
    var comment = $("#comTextArea").val()
    if (!comment) {
      return
    }
    var data = {
      userId: userId,
      comment: comment
    }
    // 发送请求
    // 重新渲染评论区域
  })

  function getInfo() {
    var data = {
      title: '病例标题XXXXXXXX',
      time: '2018-10026 15:30',
      zanCount: 56,
      isZan: false,
      content: [{
        subtitle: "病人基本信息",
        details: [
          "张三",
          " 性别： 未知"
        ]
      }, {
        subtitle: "主诉",
        details: [
          "safhjesfihodgihjiedhaeih"
        ]
      }, {
        subtitle: "现病史",
        details: [
          "safhjesfihodgihjiedhaeih",
          "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
        ]
      }],
      comments: [{
        name: 'name',
        time: '2018-10-16 11:15:53',
        content: '学习了这种疗法确实病人很好',
        zanCount: 12,
        replay: 2
      }]
    }
    // 渲染头部
    var topHtml = template("detail-top-tmpl", data)
    $("#detail-top").html(topHtml)
    // 渲染内容区域
    var contentHtml = template("content-tmpl", data)
    $("#record-content").html(contentHtml)
    // 渲染评论列表
    var commentsHtml = template("comments-tmpl", data)
    $("#comments-list").html(commentsHtml)

  }

  // 获取url中的参数
  function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) {
        theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
      }
    }
    return theRequest;
  }


})